package com.koron.system.orderAsset.controller;

import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.system.orderAsset.bean.OrderAsset;
import com.koron.system.orderAsset.service.OrderAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "工单资产")
@Slf4j
@RequestMapping("orderAsset")
@RestController
public class OrderAssetController {

    @Autowired
    private OrderAssetService assetService;

    @ApiOperation("根据id删除工单资产")
    @GetMapping("delete/{id}")
    public Response deleteOrderAsset(@PathVariable(value = "id")String id){
        Assert.notNull(id,"id不能为空！");
        int result = assetService.deleteOrderAsset(id);
        return result > 0 ? Response.ok() : Response.fail();
    }

    @ApiOperation("保存工单资产")
    @PostMapping("save")
    public Response saveOrderAsset(@RequestBody OrderAsset orderAsset){
        Assert.notNull(orderAsset,"orderAsset对象不能为空！");
        String id = assetService.insertOrderAsset(orderAsset);
        return Response.ok(id);
    }

    @ApiOperation("批量保存工单资产")
    @PostMapping("saveBatch")
    public Response saveBatchOrderAsset(@RequestBody List<OrderAsset> orderAssets){
        Assert.notEmpty(orderAssets,"orderAssets不能为空！");
        assetService.insertBatchOrderAsset(orderAssets);
        return Response.ok();
    }

    @ApiOperation("根据id获取工单资产")
    @GetMapping("get/{id}")
    public Response getOrderAssetById(@PathVariable String id){
        Assert.notNull(id,"id不能为空！");
        OrderAsset orderAsset = assetService.selectOrderAssetById(id);
        return Response.success(orderAsset);
    }

    @ApiOperation("多条件查询工单资产")
    @GetMapping("query")
    public Response getOrderAssetByQuery(OrderAsset orderAsset, PageQuery pageQuery){
        Assert.notNull(orderAsset,"orderAsset不能为空！");
        PageInfo pageInfo = assetService.selectByOrderAssetQuery(orderAsset, pageQuery);
        return Response.success(pageInfo);
    }

    @ApiOperation("更新工单资产")
    @PostMapping("update")
    public Response updateOrderAsset(@RequestBody OrderAsset orderAsset){
        Assert.notNull(orderAsset,"orderAsset不能为空！");
        int result = assetService.updateOrderAsset(orderAsset);
        return result > 0 ? Response.ok() : Response.fail();
    }

}
